package Leetcode;

import java.util.Stack;

public class isValid {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (c == '(' || c == '{' || c == '['){
                stack.push(c);
            } else {
                if (stack.isEmpty()){
                    return false;
                }
                Character top = stack.pop();
                switch (charArray[i]) {
                    case ')':
                        if (!top.equals('(')){
                            return false;
                        }
                        break;
                    case '}':
                        if (!top.equals('{')){
                            return false;
                        }
                        break;
                    case ']':
                        if (!top.equals('[')){
                            return false;
                        }
                        break;
                }
            }
        }
        return stack.isEmpty();
    }
}
